package com.itheima.edu.info.manage.controller;

import com.itheima.edu.info.manage.domain.student;
import com.itheima.edu.info.manage.service.studentService;

import java.util.Scanner;

public class studentController {
    private studentService studentService = new studentService();
    private Scanner sc = new Scanner(System.in);

    public void start() {
        studentloop:
        while (true) {

            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addstudent();
                    break;
                case "2":
                    deletestudentByid();
                    break;
                case "3":
                    updatestudent();
                    break;
                case "4":
                    findAllstudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统");
                    break studentloop;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }

    }

    public void updatestudent() {
        student[] stus = studentService.findAllstudent();
        if (stus == null) {
            System.out.println("没有老师可以修改");
            return;
        }
        String updateid = inputstudentid();
        student newstu = inputstudentinfo(updateid);
        studentService.updatestudent(updateid, newstu);
        System.out.println("修改成功");

    }

    public void deletestudentByid() {
        student[] stus = studentService.findAllstudent();
        if (stus == null) {
            System.out.println("没有老师可以删除");
            return;
        }
        String delid = inputstudentid();
        studentService.deletestudentByid(delid);
        System.out.println("删除成功!");
    }

    public void findAllstudent() {
        student[] stus = studentService.findAllstudent();
        if (stus == null) {
            System.out.println("查无信息，请添加后重试");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            student stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getSid() + "\t\t\t" + stu.getName() + "\t " + stu.getAge() + "\t   " + stu.getBirthday());
            }
        }
    }

    public void addstudent() {

        Scanner sc = new Scanner(System.in);
        String id;
        while (true) {
            System.out.println("请输入学生id:");
            id = sc.next();
            boolean flag = studentService.isExist(id);
            if (flag) {
                System.out.println("id已经被占用，请重新输入");
            } else {
                break;
            }
        }

        student stu = inputstudentinfo(id);

        boolean result = studentService.addstudent(stu);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    public String inputstudentid() {
        String id;
        while (true) {
            System.out.println("请输入学生id:");
            id = sc.next();
            boolean exists = studentService.isExist(id);
            if (!exists) {
                System.out.println("您输入的id不存在，请重新输入：");
            } else {
                break;
            }
        }
        return id;
    }

    public student inputstudentinfo(String id) {
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        String age = sc.next();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();
        student stu = new student();
        stu.setSid(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        return stu;
    }
}
